package com.eastfair.venuebooking.config.task;

import cn.hutool.core.date.DateUtil;
import com.eastfair.core.context.ContextUtil;
import com.eastfair.venuebooking.service.ContractManageService;
import com.eastfair.venuebooking.vo.ContractManageVO;
import core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 合同 提醒消息发送任务.
 *
 * @author dqq
 * @date 2023 /2/23
 */
@Slf4j
@Component
public class ContractManageMsgTask {

    @Value("${com.eastfair.projectId}")
    private Long projectId;

    @Resource
    private ContractManageService contractManageService;

    /**
     * 合同付款计划到期支付站内信提醒.
     */
    @XxlJob("sendInnerMsgWhenPayPlanDeadLine")
    public void sendInnerMsgWhenPayPlanDeadLine() {
        log.info("sendInnerMsgWhenPayPlanDeadLine - 合同付款计划到期支付站内信提醒任务开始");
        ContextUtil.setProjectId(projectId);
        Date now = new Date();
        String beginOfDate = DateUtil.beginOfDay(now).toString();
        String endOfDate = DateUtil.endOfDay(now).toString();
        List<ContractManageVO> contractManageList = contractManageService.listByPlanPayTime(beginOfDate, endOfDate);
        if (contractManageList == null || contractManageList.isEmpty()) {
            log.info("sendInnerMsgWhenPayPlanDeadLine - 没有要发送消息的合同");
            return;
        }
        for (ContractManageVO contractManageVO: contractManageList) {
            contractManageService.asyncSendInnerMsgWhenPayPlanDeadLine(contractManageVO);
        }
    }
}